home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / CODECS.ZIP / codecs / english / dcodrle2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-13  |  3.2 KB  |  99 lines

  1. /* File: dcodrle2.c
  2.    Author: David Bourgin
  3.    Creation date: 1/2/94
  4.    Last update: 22/5/94
  5.    Purpose: Example of RLE type 2 decoding with a file source to decompress.
  6. */
  7.  
  8. #include <stdio.h>
  9. /* For routines printf,fgetc and fputc */
  10. #include <stdlib.h>
  11. /* For routine exit */
  12.  
  13. /* Error codes sent to the caller */
  14. #define NO_ERROR      0
  15. #define BAD_FILE_NAME 1
  16. #define BAD_ARGUMENT  2
  17.  
  18. /* Useful constants */
  19. #define FALSE 0
  20. #define TRUE  1
  21.  
  22. /* Global variables */
  23. FILE *source_file,*dest_file;
  24.  
  25.                              /* Being that fgetc=EOF only after an access
  26.                                 then 'byte_stored_status' is 'TRUE' if a byte has been stored by 'fgetc'
  27.                                 or 'FALSE' if there's no valid byte not already read and not handled in 'val_byte_stored' */
  28. int byte_stored_status=FALSE;
  29. int val_byte_stored;
  30.  
  31. /* Pseudo procedures */
  32. #define end_of_data()  (byte_stored_status?FALSE:!(byte_stored_status=((val_byte_stored=fgetc(source_file))!=EOF)))
  33. #define read_byte()  (byte_stored_status?byte_stored_status=FALSE,(unsigned char)val_byte_stored:(unsigned char)fgetc(source_file))
  34. #define write_byte(byte)  ((void)fputc((byte),dest_file))
  35.  
  36. void rle2decoding()
  37. /* Returned parameters: None
  38.    Action: Decompresses with RLE type 2 method all bytes read by the function read_byte
  39.    Errors: An input/output error could disturb the running of the program
  40. */
  41. { unsigned char header_byte,byte_read,byte_to_repeat;
  42.   register unsigned int i;
  43.  
  44.   if (!end_of_data())
  45.      { header_byte=read_byte();
  46.        do {                  /* Being that header byte is present, then there are bytes to decompress */
  47.             if ((byte_read=read_byte())==header_byte)
  48.                { byte_read=read_byte();
  49.                  if (byte_read<3)
  50.                     byte_to_repeat=header_byte;
  51.                  else byte_to_repeat=read_byte();
  52.                  for (i=0;i<=byte_read;i++)
  53.                      write_byte(byte_to_repeat);
  54.                }
  55.             else write_byte(byte_read);
  56.           }
  57.        while (!end_of_data());
  58.      }
  59. }
  60.  
  61. void help()
  62. /* Returned parameters: None
  63.    Action: Displays the help of the program and then stops its running
  64.    Errors: None
  65. */
  66. { printf("This utility enables you to decompress a file by using RLE type 2 method\n");
  67.   printf("as given in 'La Video et Les Imprimantes sur PC'\n");
  68.   printf("\nUse: dcodrle2 source target\n");
  69.   printf("source: Name of the file to decompress\n");
  70.   printf("target: Name of the restored file\n");
  71. }
  72.  
  73. int main(argc,argv)
  74. /* Returned parameters: Returns an error code (0=None)
  75.    Action: Main procedure
  76.    Errors: Detected, handled and an error code is returned, if any
  77. */
  78. int argc;
  79. char *argv[];
  80. { if (argc!=3)
  81.      { help();
  82.        exit(BAD_ARGUMENT);
  83.      }
  84.   else if ((source_file=fopen(argv[1],"rb"))==NULL)
  85.           { help();
  86.             exit(BAD_FILE_NAME);
  87.           }
  88.        else if ((dest_file=fopen(argv[2],"wb"))==NULL)
  89.                { help();
  90.                  exit(BAD_FILE_NAME);
  91.                }
  92.             else { rle2decoding();
  93.                    fclose(source_file);
  94.                    fclose(dest_file);
  95.                  }
  96.   printf("Execution of dcodrle2 completed.\n");
  97.   return (NO_ERROR);
  98. }
  99.